package com.example.androidleanbackdemo;

import static androidx.leanback.widget.BaseGridView.FOCUS_SCROLL_ALIGNED;
import static androidx.leanback.widget.BaseGridView.FOCUS_SCROLL_ITEM;
import static androidx.leanback.widget.BaseGridView.FOCUS_SCROLL_PAGE;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.FocusHighlightHelper;
import androidx.leanback.widget.HorizontalGridView;
import androidx.leanback.widget.ItemBridgeAdapter;
import androidx.leanback.widget.OnChildViewHolderSelectedListener;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidleanbackdemo.presenter.HPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: njb
 * @date: 2023/3/30 0:31
 * @desc:
 */
public class LeanBackActivity extends AppCompatActivity {
    public static final String TAG="MainActivity";
    private HorizontalGridView horizontalGridView;
    private List<Integer> mDataList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leanback);
        initViews();
    }

    private void initViews() {
        horizontalGridView= findViewById(R.id.hgv);
        //4行
        horizontalGridView.setNumRows(3);
        //item纵向和横向的距离
        horizontalGridView.setItemSpacing(40);
        //item的对齐方式
        horizontalGridView.setGravity(Gravity.CENTER_VERTICAL);
        //设置
        horizontalGridView.setOnChildViewHolderSelectedListener(new OnChildViewHolderSelectedListener() {
            @Override
            public void onChildViewHolderSelected(RecyclerView parent, RecyclerView.ViewHolder child, int position, int subposition) {
                super.onChildViewHolderSelected(parent, child, position, subposition);
                Log.d(TAG, "onChildViewHolderSelected() returned: " + position);
                //大部分情况下可以通过该方法获取到position

            }

            @Override
            public void onChildViewHolderSelectedAndPositioned(RecyclerView parent, RecyclerView.ViewHolder child, int position, int subposition) {
                super.onChildViewHolderSelectedAndPositioned(parent, child, position, subposition);
                Log.d(TAG, "onChildViewHolderSelectedAndPositioned() returned: " + position);
                //当通过setSelectedPosition()方法大幅移动列表时，该方法会回调，返回的是最终的真实的position（当set的值超出范围时...)
            }
        });
        HPresenter presenter=new HPresenter();
        //创建ObjectAdapter，用于提供数据，当有多种类型时，传入PresenterSelector
        ArrayObjectAdapter objectAdapter=new ArrayObjectAdapter(presenter);
        //初始化模拟数据
        initData();
        //添加数据
        objectAdapter.addAll(0,mDataList);
        //通过前面创建的objectAdapter创建ItemBridgeAdapter，完成数据的传递
        ItemBridgeAdapter bridgeAdapter=new ItemBridgeAdapter(objectAdapter);
        //将ItemBridgeAdapter传入HorizontalGridView
        horizontalGridView.setAdapter(bridgeAdapter);
        horizontalGridView.requestFocus();
        //设置上焦动画
        FocusHighlightHelper.setupHeaderItemFocusHighlight(bridgeAdapter);
    }

    private void initData(){
        if (mDataList==null){
            mDataList=new ArrayList<>();
            for (int i = 0; i <100 ; i++) {
                mDataList.add(i);
            }
        }
    }

}
